package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.CommunicationProtocol;
import com.sskj.api.exception.LogicException;
import com.sskj.api.mapper.CommunicationProtocolMapper;
import com.sskj.api.service.ICommunicationProtocolService;
import com.sskj.api.vo.req.CommunicationProtocolPageListReq;
import com.sskj.common.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CommunicationProtocolService extends ServiceImpl<CommunicationProtocolMapper, CommunicationProtocol> implements ICommunicationProtocolService {

    @Override
    public void add(CommunicationProtocol communicationProtocol) {
        LambdaQueryWrapper<CommunicationProtocol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunicationProtocol::getCode, communicationProtocol.getCode())
                .or()
                .eq(CommunicationProtocol::getName, communicationProtocol.getName());

        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("通信协议名称或编码已存在");
        }

        communicationProtocol.setCreateTime(LocalDateTime.now());
        save(communicationProtocol);
    }

    @Override
    public void update(CommunicationProtocol communicationProtocol) {
        LambdaQueryWrapper<CommunicationProtocol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(CommunicationProtocol::getId, communicationProtocol.getId())
                .and(consumer -> {
                    consumer.eq(CommunicationProtocol::getCode, communicationProtocol.getCode())
                            .or()
                            .eq(CommunicationProtocol::getName, communicationProtocol.getName());
                });

        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("通信协议名称或编码已存在");
        }

        // 禁止修改CODE，会影响系统查询
        CommunicationProtocol protocol = getById(communicationProtocol.getId());
        communicationProtocol.setCode(protocol.getCode());

        communicationProtocol.setUpdateTime(LocalDateTime.now());
        updateById(communicationProtocol);
    }

    @Override
    public IPage<CommunicationProtocol> pageList(CommunicationProtocolPageListReq req) {
        LambdaQueryWrapper<CommunicationProtocol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(CommunicationProtocol::getId);
        if (StrUtil.isNotBlank(req.getName())) {
            lambdaQueryWrapper.like(CommunicationProtocol::getName, req.getName());
        }
        if (req.getStatus() != null && req.getStatus() >= 0) {
            lambdaQueryWrapper.eq(CommunicationProtocol::getStatus, req.getStatus());
        }

        IPage<CommunicationProtocol> pageConfig = new Page<>(req.getPageNum(), req.getPageSize());
        return page(pageConfig, lambdaQueryWrapper);
    }

    @Override
    public void batchAdd(List<CommunicationProtocol> communicationProtocols) {
       LambdaUpdateWrapper<CommunicationProtocol> updateWrapper = new LambdaUpdateWrapper<>();
       updateWrapper.set(CommunicationProtocol::getStatus, 0);
       update(updateWrapper);

       if (communicationProtocols != null && !communicationProtocols.isEmpty()) {
           for (CommunicationProtocol communicationProtocol : communicationProtocols) {
               CommunicationProtocol communicationProtocol1 = getByCode(communicationProtocol.getCode());
               if (communicationProtocol1 == null) {
                   communicationProtocol.setCreateTime(LocalDateTime.now());
                   communicationProtocol.setStatus(1);
                   save(communicationProtocol);
               } else {
                   communicationProtocol1.setName(communicationProtocol.getName());
                   communicationProtocol1.setDescription(communicationProtocol.getDescription());
                   communicationProtocol1.setUpdateTime(LocalDateTime.now());
                   communicationProtocol1.setStatus(1);

                   updateById(communicationProtocol1);
               }
           }
       }
    }

    /**
     * 通过Code获取实例
     * @param code 编码
     * @return 实例
     */
    private CommunicationProtocol getByCode(String code) {
        LambdaQueryWrapper<CommunicationProtocol> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunicationProtocol::getCode, code);
        return getOne(lambdaQueryWrapper);
    }
}
